package com.lwj.liwanjia.utils;

import android.os.CountDownTimer;
import android.widget.Button;
import android.widget.TextView;

/**
 * 倒计时
 */

public class TimeCountDown extends CountDownTimer {
    private TextView tvHour;
    private TextView tvMinute;
    private TextView tvSecond;

    /**
     * @param millisInFuture    总共的时间戳
     * @param countDownInterval 每次减少的时间，一般为1000
     * @param
     */
    public TimeCountDown(long millisInFuture, long countDownInterval, TextView tvHour, TextView tvMinute, TextView tvSecond) {
        super(millisInFuture, countDownInterval);
        this.tvHour = tvHour;
        this.tvMinute = tvMinute;
        this.tvSecond = tvSecond;
        tvHour.setClickable(false);
        tvMinute.setClickable(false);
        tvSecond.setClickable(false);
    }

    @Override
    public void onTick(long millisUntilFinished) {
        long l = millisUntilFinished / 1000;
        //  long day = l / (24 * 60 * 60);
        long hours = (l % (24 * 60 * 60)) / (60 * 60);
        long minutes = ((l % (24 * 60 * 60)) % (60 * 60)) / 60;
        long second = ((l % (24 * 60 * 60)) % (60 * 60)) % 60;
        //   String dayStr = String.valueOf(day);
        String hoursStr = timeStrFormat(String.valueOf(hours));
        String minutesStr = timeStrFormat(String.valueOf(minutes));
        String secondStr = timeStrFormat(String.valueOf(second));

        tvHour.setText(hoursStr);
        tvMinute.setText(minutesStr);
        tvSecond.setText(secondStr);
    }

    @Override
    public void onFinish() {
        tvHour.setText("00");
        tvMinute.setText("00");
        tvSecond.setText("00");
    }

    private static String timeStrFormat(String timeStr) {
        switch (timeStr.length()) {
            case 1:
                timeStr = "0" + timeStr;
                break;
        }
        return timeStr;
    }

}
